package skyinline.swing;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class SwingPopupMenu extends JPopupMenu {

	private JPopupMenu popup = null;
	
	public SwingPopupMenu( JPanel p ) {
		//	팝업메뉴 생성
		popup = new JPopupMenu();
		JMenuItem m1 = new JMenuItem("new");
		JMenuItem m2 = new JMenuItem("close");
		popup.add(m1);
		popup.add(m2);
		
		p.addMouseListener(new PopupMenuListener());	
		//	SwingMain의 contentPane(JPanel) 에
		//	PopupMenuListener 를 MouseListener 로 등록
	}
	
	class PopupMenuListener extends MouseAdapter {
		
		public void mousePressed(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showMenu(e);
			}
		}
		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showMenu(e);
			}
		}
		private void showMenu(MouseEvent e) {
			popup.show(e.getComponent(), e.getX(), e.getY());
		}
		
	}
	
}
